package com.lin.generator;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ArrayUtil;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;

public class StaticFileGenerator {
    public static void StaticFileGenerator() {
        //相对路径
        String property = System.getProperty("user.dir");
        System.out.println("当前路径："+property);
        //输入路径
        String inputpath = property+File.separator+"code-generate"+File.separator+"acm-template";
        //输出路径
        String outputpath = property;
        //复制文件
        CopyFileByHutool(inputpath,outputpath);
        //复制文件
        CopyFilesByRecursion(inputpath,outputpath);
    }

    /**
     * 使用Hutool复制文件
     * 整体复制，整个文件夹全部复制过去
     * @param inputpath
     * @param outputpath
     */
    public static void CopyFileByHutool(String inputpath,String outputpath){
        FileUtil.copy(inputpath,outputpath,false);
    }

    /**
     * 使用递归的方式复制文件
     * @param inputpath
     * @param outputpath
     */
    public static void CopyFilesByRecursion(String inputpath,String outputpath) {
        File inputFile = new File(inputpath);
        File outputFile = new File(outputpath);
        try {
            CopyFileByRecursion(inputFile,outputFile);
        }catch (Exception e){
            System.out.println("文件复制异常");
            e.printStackTrace();
        }
    }

    private static void CopyFileByRecursion(File inputFile, File outputFile) throws IOException {
        //判断是文件还是目录
        if (inputFile.isDirectory()){
            System.out.println(inputFile.getName());
            File destFile = new File(outputFile, inputFile.getName());
            //如果是目录，创建目录
            // 如果是目录，首先创建目标目录
            if (!destFile.exists()) {
                destFile.mkdirs();
            }
            File[] files = inputFile.listFiles();
            if(ArrayUtil.isEmpty(files)){
                return;
            }
            for (File file : files) {
                CopyFileByRecursion(file,destFile);
            }
        }else {
            //如果是文件，复制文件
            Path destPath = outputFile.toPath().resolve(inputFile.getName());
            Files.copy(inputFile.toPath(), destPath, StandardCopyOption.REPLACE_EXISTING);
        }
    }

}
